package com.glory.brepda.activity.mfg;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.MantissaLotListAdapter;
import com.glory.brepda.adapter.SampleListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.LotList;
import com.glory.brepda.entity.SimpleEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * FileName: FTTrackOutBiandaiErrorActivity
 * Author: XJH
 * Date: 2020/5/22 10:15
 * Description:FT测试编带出站，异常出站
 * History:
 */
public class FTTrackOutBiandaiErrorActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvMessage)
    TextView tvMessage;
    @BindView(R.id.tvLot)
    TextView tvLot;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    @BindView(R.id.tvLotQTY)
    TextView tvLotQTY;
    @BindView(R.id.list_titleLayout)
    LinearLayout listTitleLayout;
    @BindView(R.id.rvTestList)
    RecyclerView rvTestList;
    @BindView(R.id.rvItemLotList)
    RecyclerView rvItemLotList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvSubmit1)
    TextView tvSubmit1;
    private MantissaLotListAdapter mTestAdapter;
    private MantissaLotListAdapter mMantissaLotListAdapter;
    private String mEqpid;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_track_out_biandai_error_ft;
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        setMainLayout(llMain);
        addActivity(this);
        tvTitle.setText("异常结束-(FT测试)");
        tvLot.setText(bundle.getString("LOTID"));
        tvPackageType.setText(bundle.getString("PACKAGETYPE"));
        mEqpid = bundle.getString("EQPID", "");
        tvLotQTY.setText(bundle.getString("LOTNUM"));
        initRecyclerView(rvItemLotList,this);
        initRecyclerView(rvTestList,this);
        mTestAdapter = new MantissaLotListAdapter(null,true);
        mTestAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mTestAdapter.isFirstOnly(true);
        rvTestList.setAdapter(mTestAdapter);
        mMantissaLotListAdapter = new MantissaLotListAdapter(null,true);
        mMantissaLotListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mMantissaLotListAdapter.isFirstOnly(true);
        rvItemLotList.setAdapter(mMantissaLotListAdapter);
    }

    @Override
    protected void initData() {
        requestData("SelectBinData",false);
    }

    @OnClick({R.id.tvBaseSubmit,R.id.tvSubmit1})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                requestData("AbnormalTrackOut",false);
                break;
            case R.id.tvSubmit1:
                requestData("AbnormalTrackOut",true);
                break;
        }

    }

    private void requestData(String actionType,boolean IsStayendlot){
        FreeUI_EntityCallBack<BaseResponseBean<SimpleEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<SimpleEntity>>
                (new TypeToken<BaseResponseBean<SimpleEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<SimpleEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    SimpleEntity entity = response.body().getResponse().getBody();
                    if (actionType.equals("AbnormalTrackOut")){
                        showConfirmDialogFinishAll(entity.getTRACKSHOWMSG().equals("") ? "出站成功" : entity.getTRACKSHOWMSG());
                        return;
                    }
                    if (!entity.getTRACKSHOWMSG().equals("")){
                        showConfirmDialog(entity.getTRACKSHOWMSG());
                    }
                    if (entity.getLOTBINDATALIST().size() <= 0){
                        showToast("暂无测试数据",false);
                    }else if (entity.getMANTISSALOTBINDATALIST().size() <= 0){
                        showToast("暂无尾数批次信息",false);
                    }
                    mTestAdapter.setNewData(entity.getLOTBINDATALIST());
                    mMantissaLotListAdapter.setNewData(entity.getMANTISSALOTBINDATALIST());
                }


            }

            @Override
            public void onError(Response<BaseResponseBean<SimpleEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "LOTFTTRACKOUT");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", actionType);
        map.addProperty("EQUIPMENTID", mEqpid);
        map.addProperty("STAYENDLOT", IsStayendlot);
        List<LotList> lotList = new ArrayList<>();
        lotList.add(new LotList(tvLot.getText().toString()));
        map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());
        NetUtils.requestPostNet("IF.LOTFTTRACKOUT", "LOTFTTRACKOUT", FTTrackOutBiandaiErrorActivity.this, map, callBack);
    }

}
